import 'package:eh_mobile/common/utils/extension.dart';
import 'package:flutter/material.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:get/get.dart';
import 'package:logger/logger.dart';

import 'index.dart';

// 家庭设置
class SetFamilyPage extends GetView<SetFamilyController> {
  const SetFamilyPage({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(14),
      child: KeyboardDismissOnTap(
          dismissOnCapturedTaps: true, // 点击任意地方关闭键盘
          child: Column(
            children: [
              Container(
                alignment: Alignment.centerLeft,
                child: const Text('名称',
                    textAlign: TextAlign.left,
                    style: TextStyle(color: Color(0xff303133), fontSize: 16)),
              ),
              10.sizedBoxHeight,
              _buildTopTextField(context),
              20.sizedBoxHeight,
              _buildListView()
            ],
          )),
    );
  }

  //创建输入框
  Widget _buildTopTextField(BuildContext context) {
    return Container(
      height: 40,
      alignment: Alignment.center,
      child: TextField(
        maxLines: 1,
        controller: controller.inputController,
        focusNode: controller.inputFocusNode,
        textInputAction: TextInputAction.done,
        decoration: InputDecoration(
          hintText: '我的家',
          isCollapsed: true, //高度包裹
          contentPadding: const EdgeInsets.symmetric(
              horizontal: 8, vertical: 10), //内容内边距，影响高度
          border: controller.outlineInputBorder,
          focusedBorder: controller.outlineInputBorder,
          enabledBorder: controller.outlineInputBorder,
        ),
        textAlign: TextAlign.left,
        onSubmitted: (value) {
          //提交按钮点击回调
          Logger().d(value);
          FocusScope.of(context).requestFocus(FocusNode());
        },
        onEditingComplete: () {
          // 输入完成回调
        },
      ),
    );
  }

  Widget _buildListView() {
    return Container(
      decoration: const BoxDecoration(
        color: Color(0xfff2f3f5),
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      height: 54.0 * controller.listTitles.length,
      child: ListView.builder(
        itemBuilder: (context, index) {
          return _buildListViewItem(context, index);
        },
        itemExtent: 54,
        physics: const NeverScrollableScrollPhysics(),
        itemCount: controller.listTitles.length,
      ),
    );
  }

  // 列表子视图
  Widget _buildListViewItem(
    BuildContext context,
    int index,
  ) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        SizedBox(
          height: 53,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              10.sizedBoxWidth,
              Text(
                controller.listTitles[index],
                style: const TextStyle(fontSize: 16, color: Color(0xff303133)),
              ),
              10.sizedBoxWidth,
              Expanded(
                  child: Text(
                controller.listContents[index],
                textAlign: TextAlign.right,
                style: const TextStyle(fontSize: 16, color: Color(0xff303133)),
              )),
              const Icon(Icons.keyboard_arrow_right),
              10.sizedBoxWidth,
            ],
          ).onClick(() {
            controller.listViewItemClick(index);
          }),
        ),
        Container(
          margin: const EdgeInsets.fromLTRB(14, 0, 14, 0),
          color: const Color.fromARGB(255, 190, 185, 185),
          height: (index < controller.listTitles.length - 1) ? 1 : 0,
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SetFamilyController>(
      init: SetFamilyController(),
      id: "set_family",
      builder: (_) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('家庭设置'),
          ),
          body: SafeArea(
            child: _buildView(context),
          ),
        );
      },
    );
  }
}
